
//点击搜索查询
$('#search').click(function(){
    $.ajax({
        url: "/citrusmonitor/disease.do?method=searchDisease",
        data: {
            "word": $('#disease_search').val()//编号
        },
        type: "post",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        success: function (data) {
        	
        	if(data.list==null){
                $('#tbody').html('');
                return;
            }
        	 
            var html = '';
            console.log(data)
      $.each(data.list,function(i,value){
    	  if(value["content"].length>=20){
          	value["content"] = value["content"].substring(0, 21) + "....";
          }
    	  html +="<tr><td><input type=\"checkbox\" value="+value["diseaseId"]+" name=\"ids\"><input type=\"hidden\" value="+value["diseaseId"]+"></td>"
			  		+"<td>"+(i+1)+"</td>"
			          +"<td>"+value["diseaseName"]+"</td>"
			          + "<td>"+value["content"]+"</td>"
			          +"<td>"+value["category"]+"</td>"
			          + "<td>"+value["distribution"]+"</td>"
			          +" <td>"+value["imgPath"]+"</td>"
			          +" <td>"
			          +"</span id=\"edit\"><span><a href=\"edit.html?diseaseId="+value["diseaseId"]+"\" target=\"rightframe\">"+"<i class=\"iconfont\">&#xe7e0;</i></a></span>"
			          +"<span class=\"delete\"  onclick='deleteBtn(this)'><i class=\"iconfont\">&#xe695;</i></span>"
			          +"</td></tr>"
      });
      
            $('#tbody').html(html);
            
        },
        error:function(){
            alert("资源加载失败！！！");
        }
    });
})


//  单个删除
function deleteBtn(obj)
{
//    var userId=obj.parentNode.parentNode.firstChild.innerText;//得到按钮[obj]的父元素[td]的父元素[tr]
	 var diseaseId = obj.parentNode.parentNode.getElementsByTagName('input')[1].value;
    console.log(diseaseId);
    // tr.parentNode.removeChild(tr);//从tr的父元素[tbody]移除tr
    $.ajax({
        url: "/citrusmonitor/disease.do?method=remove&diseaseId="+diseaseId,
        data: $.param({diseaseId:diseaseId}),
        type: "post",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        dataType: "json",
        success: function (res) {
        	 if (res.status==1) {
        		 alert("删除成功！！！");
                 location.reload();
             } else {
                 alert("删除失败" );
             }
           
        }
    });
}

//刷新
$('#refresh').click(function(){
	location.reload();
})


//全选按钮
 $("#allChk").click(function() {  
            $("input[name='subChk']").prop("checked",this.checked);  
        });  
          
        // 单选  
        var subChk = $("input[name='subChk']")  
        subChk.click(function() {  
            $("#allChk").prop("checked", subChk.length == subChk.filter(":checked").length ? true:false);  
        });  
          
        /* 批量删除 */  
        $("#del_model").click(function() {  
            // 判断是否至少选择一项  
            var checkedNum = $("input[name='subChk']:checked").length;  
            if(checkedNum == 0) {  
                alert("请选择至少一项！");  
                return;  
            }  
              
            // 批量选择   
            if(confirm("确定要删除所选项目？")) {  
                var checkedList = new Array();  
                $("input[name='subChk']:checked").each(function() { 
                    checkedList.push($(this).next().val());
                });  
                console.log(checkedList);
                $.ajax({  
                    type: "POST",  
                    url: "/citrusmonitor/disease.do?method=deleteInBatch",  
                    data: {'ids':checkedList.toString()},
                    success: function(msg) {
                    	  msg = JSON.parse(msg);
                    	console.log(msg.status)
                    	if(msg.status){
                    		$("[name ='subChk']:checkbox").attr("checked", false);
                    		alert("删除成功！！！");
                            window.location.reload();  
                    	}else{
                    		alert("删除失败！！！");
                    	}
                        
                    }  
                });  
            }             
        });  



